package be.groept.ace.cursusplanning.persistency;

/**
 * @author Gouke Leplat
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

import Log.Log;


public class Inventaris {
	
	static String DataMap;
	static File inventarisDocument;
	IPersistentieController ctrl;
	PrintWriter mainWriter;
	Scanner mainScanner;
	
	public Inventaris(IPersistentieController ctrl, String dataMap){
		Inventaris.DataMap = dataMap;
		this.ctrl = ctrl;

		try{
			instatiateInventaris();
		}
		catch (IOException e)
		{
			// prompt voor ander Invenatrisbestand
			System.out.print("ERROR: Inventaris could not instantiate");
		}
	}
	
	private void instatiateInventaris() throws IOException{
		
		inventarisDocument = new File(DataMap + "Inventaris.txt");
		
		if (inventarisDocument.createNewFile()){
			openWriter();
			mainWriter.println("@VrijeCursist;");
			mainWriter.println("@EndVrijeCursist;");
			mainWriter.println("@HerscholingCursist;");
			mainWriter.println("@EndHerscholingCursist;");
			mainWriter.println("@Cursus;");
			mainWriter.println("@EndCursus;");
			mainWriter.println("@Inschrijving;");
			mainWriter.println("@EndInschrijving;");
			mainWriter.println("@Lector;");
			mainWriter.println("@Endlector;");
			
			closeWriter();
		}
	}
	
	public boolean existInInventaris(Persisteerbaar obj){
		
		Scanner wholeRule = null;
		String partOfRule = null;
		String objID = ctrl.getObjectKey(obj);
		
		this.openScanner();
		
		boolean hit = false;
		
		// bestaat reeds in index?
		while (hit == false && mainScanner.hasNext()){
			wholeRule = new Scanner(mainScanner.nextLine());
			wholeRule.useDelimiter(";");
			
			partOfRule = wholeRule.next();
			
			if (partOfRule.equalsIgnoreCase(objID)){
				hit = true;
				break;
			}
		}
		
		closeScanner();
		return hit;
	}

	public ArrayList<String> getListOfAllObjects(){
		
		ArrayList<String> lijst = new ArrayList<String>();
		Scanner littleScanner = null;
		
		this.openScanner();
		
		String lijn;
		
		Log.LOG("-----------------INVENTARIS----------------------", 2);
		
		//littleScanner = new Scanner(mainScanner.nextLine());
		while (mainScanner.hasNextLine()){
			
			littleScanner = new Scanner( mainScanner.nextLine());
			littleScanner.useDelimiter(";");
			lijn = littleScanner.next();
			System.out.println("Lijn: " + lijn);
			if (!lijn.substring(0,1).equalsIgnoreCase("@") ){

				lijst.add(lijn);					
			}
		}
		
		Log.LOG("---------------------EINDE INVENTARIS-------------------",2);
		this.closeScanner();
		
		return lijst;
	}
	
 	public void addToInventaris(Persisteerbaar obj) throws Exception{
		
		if (existInInventaris(obj)){
			throw new Exception ("Inventaris: Object bestaat reeds in inventaris");
		}
		
		Scanner wholeRule = null;
		String partOfRule = null;
		String objID = ctrl.getObjectKey(obj);
		
		this.openScanner();
		
		// komt als laatste object in zijn categorie
		boolean before = true;
		ArrayList<String> voor = new ArrayList<String>();
		ArrayList<String> na = new ArrayList<String>();
		
		openScanner();

		while (mainScanner.hasNext()) 
		{
			wholeRule = new Scanner(mainScanner.nextLine());
			wholeRule.useDelimiter(";");
			partOfRule = wholeRule.next();
			
			if (partOfRule.equalsIgnoreCase("@End" + obj.getClass().getSimpleName())) {
				before = false;
			}
			
			if (before == true){
				voor.add(partOfRule);
			}
			else{
				na.add(partOfRule);
			}
		}
		closeScanner();
		
		openWriter();
		
		for (int i = 0; i < voor.size(); i++){
			mainWriter.println(voor.get(i) +";");
		}
		
		/* Nieuw object tussenvoegen */
		mainWriter.println(objID + ";");
		
		for (int i = 0; i < na.size(); i++){
			mainWriter.println(na.get(i) + ";");
		}
		
		closeWriter();
		
	}
	public void removeFromInventaris(Persisteerbaar obj){
		//TODO: removeFromInventaris: Niet echt nodig. Zou mooi zijn om compleet te zijn
	}
	

	
	/* Pivate tools */
	private void openScanner() {
		try{
			mainScanner = new Scanner(inventarisDocument);
			mainScanner.useDelimiter(";");
		}
		catch (FileNotFoundException e){
			// NOT POSSIBLE
			System.out.println("Inventaris: Openscanner: FileNotFoundEx");
		}
		
	}
	private void closeScanner(){
		mainScanner.close();
		mainScanner = null;
	}
	private void openWriter(){
		try{
			mainWriter = new PrintWriter(inventarisDocument);
		}
		catch (FileNotFoundException e){
			
		}
	}
	private void closeWriter(){
		mainWriter.close();
		mainWriter = null;
	}
}
